package com.example.servlet;

import com.example.pojo.Customer;
import com.example.service.ICustomerService;
import com.example.service.impl.ICustomerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/customerServlet")
public class CRUServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8"); //设置编码

        String method = req.getParameter("method");
        if(method.equals("delete")){
            int id =Integer.parseInt(req.getParameter("cust_id"));
            ICustomerService customerService = new ICustomerServiceImpl();
            int result =  customerService.deleteCustomerById(id);

            System.out.println(result);
            req.getRequestDispatcher("/customer").forward(req,resp);
        }
        if(method.equals("edit")){
            int id =Integer.parseInt(req.getParameter("cust_id"));
            ICustomerService customerService = new ICustomerServiceImpl();
            Customer customer=customerService.findById(id);
            req.setAttribute("customer",customer);
            req.getRequestDispatcher("/WEB-INF/jsp/customer/edit.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8"); //设置编码

        String method = req.getParameter("method");
        if(method.equals("addsubmit")) {
            Customer customer=new Customer();
            customer.setName(req.getParameter("cust_name"));
            customer.setLevel(req.getParameter("cust_level"));
            customer.setSource(req.getParameter("cust_source"));
            customer.setLinkman(req.getParameter("cust_linkman"));
            customer.setPhone(req.getParameter("cust_phone"));
            customer.setMobile(req.getParameter("cust_mobile"));
            ICustomerService customerService = new ICustomerServiceImpl();
            int result =  customerService.saveCustomer(customer);
            System.out.println(result);
            req.getRequestDispatcher("/customer").forward(req,resp);
        }
        else if(method.equals("list")){
            String cust_name = req.getParameter("cust_name");
            ICustomerService customerService = new ICustomerServiceImpl();
            List<Customer>  lists=  customerService.findByLikeName(cust_name);
            System.out.println(lists);

            req.setAttribute("list",lists);

            // 3.
            req.getRequestDispatcher("/WEB-INF/jsp/customer/list.jsp").forward(req,resp);
        }
        else if(method.equals("editsubmit")) {
            Customer customer=new Customer();
            int id =Integer.parseInt(req.getParameter("custId"));

            customer.setId(id);
            customer.setName(req.getParameter("cust_name"));
            customer.setLevel(req.getParameter("cust_level"));
            customer.setSource(req.getParameter("cust_source"));
            customer.setLinkman(req.getParameter("cust_linkman"));
            customer.setPhone(req.getParameter("cust_phone"));
            customer.setMobile(req.getParameter("cust_mobile"));
            System.out.println(customer);
            ICustomerService customerService = new ICustomerServiceImpl();
            int result =  customerService.updateCustomerById(customer);
            System.out.println(result);
            req.getRequestDispatcher("/customer").forward(req,resp);
        }

    }
}
